<?php
class Content extends EMongoDocument
{
    public $title;
    public $text;
    public $type;
    public $user_id;

    public function getCollectionName()
    {
        return 'content';
    }
    
    /**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    
    public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, text, user_id, type', 'required'),
            
		);
    }
    
    public function init()
    {
        parent::init();

        //init its type.
        if($this instanceof IContentExtra)
            $this->type = $this->getTypeName();
    }
    
    /**
     * Override this method and make sure if $values(always it's $_POST) includes the key of this class
     * Ex. this class is Post and $_POST['Post'] is exists
     * @param array $values
     * @param boolean $safeOnly
     * @throws CException
     */
    public function setAttributes($values, $safeOnly = true)
    {
        if($this instanceof IContentExtra)
        {
            $className = $this->getTypeModelName();
            if(isset($values[$className]))
            {
                $value = $values[$className];
                unset($values[$className]);
                $values = array_merge($values, $value);
            }

            parent::setAttributes($values, $safeOnly);
        }
        else
            throw new CException('Must be a subclass of the content model, Ex:Post');
    }
    
    /**
     * We can override the instantiate method to return correct models
     */
    protected function instantiate($attributes)
    {
        $modelName = self::getTypeModelNameByType($attributes['type']);
        $model = new $modelName(null);   // We have to set scenario to null, it will be set, by populateRecord, later
        $model->initEmbeddedDocuments(); // We have to do it manually here!
        $model->setAttributes($attributes, false);

        if( ! ($model instanceof Content))
            return null;

        return $model;
    }

    /**
     * get the instance by type string.
     * We are not allowed to get content instance directly!
     * please using Content::getInstanceByType($type)
     * Ex:
     * $content = Content::getInstanceByType(Post::getTypeName())
     * or
     * $content = new Post;
     * @param array $attributes Ex.$_POST
     * @param boolean $runSetAttributes
     * @return Content subClass of Content
     */
    static public function getInstance($attributes, $runSetAttributes=true)
    {
        if( ! isset($attributes['type']))
            throw new CException('type can\'t be empty');

        $type = $attributes['type'];
        if(in_array($type, self::getTypeOptions()))
        {
            $modelName = self::getTypeModelNameByType($type);
			$model = $modelName::getInstanceByAttributes($attributes, $runSetAttributes=true);
            return $model;
        }
        else
            throw new CException('type must be one of Content::getTypeOptions()');
    }
    
    static public function getTypeOptions($displayOnly=false,$justConent=false)
    {
        $types = array(
            0=>'one_choice',
            1=>'multiple_choice',
        );
        
        return $types;
    }
    
    public function getType()
    {
        return $this->type;
    }

    public function getTypeModelName()
    {
        return CommonHelper::getModelName($this->type);
    }

    static public function getTypeModelNameByType($type)
    {
        return CommonHelper::getModelName($type);
    }
    
    public function getDisplayText()
    {
        return $this->text;
    }
    
    public function getDisplayTitle()
    {
        return $this->title;
    }
    
    /**
     * get the more content url.
     * @param mixed $contents To findout a right time string for setting the offset. array:the result array now, '':page link, default:now.
     * @param array $extraCriteria
     * @param array $extraParams
     * @return string moreUrl the pathinfo does not suppurt the param which is too long, using get instead.
     */
    static public function getMoreUrl($contents, $extraCriteria=array(), $extraParams=array())
    {
        $params = CommonHelper::createGetModelsUrlParams ( $contents, $extraCriteria, $extraParams );
        return Yii::app()->createUrl('content/more?offset='.$params['offset'].'&extraCriteria='.$params['extraCriteria'].'&extraParams='.$params['extraParams']);
    }
}
